<template>
  <body id="poster">
    <el-form class="register-container" label-position="left" label-width="0px">
      <h3 class="register_title">注册</h3>
      <el-form-item>
        <el-input type="text" v-model="RegisterForm.username" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
 
      <el-form-item>
        <el-input type="password" v-model="RegisterForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input type="password" v-model="RegisterForm.repassword" auto-complete="off" placeholder="确认密码"></el-input>
      </el-form-item>

    
        <el-form-item>
        <el-input type="text" v-model="RegisterForm.email" auto-complete="off" placeholder="邮箱"></el-input>
      </el-form-item>

     <el-form-item class="code_style">
        <el-button type="primary" style="width: 100%;background: #F08080;border: none" v-on:click="email_code">获取验证码</el-button>
      </el-form-item>
      

      <el-form-item>
        <el-input type="text" v-model="RegisterForm.code" auto-complete="off" placeholder="验证码" class="code_style"></el-input>
      </el-form-item>
 
      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%;background: #505458;border: none" v-on:click="register">注册</el-button>
      </el-form-item>
    </el-form>
  </body>
</template>

 
<script>
   import {register_post,my_email} from '../api/api'
    export default {
        name: "Login",
        data() {
            return {
                RegisterForm: {
                    username: '',
                    password: '',
                    repassword:'',
                    email:'',
                    code:''
                },
            }
        },
        methods: {
            email_code(){
                if(this.RegisterForm.email==''){
                    this.$message('邮箱不能为空')
                    return false;

                }else {
                   var remail=/^[\w\-\.]+@[a-z0-9]+(\-[a-z0-9]+)?(\.[a-z0-9]+(\-[a-z0-9]+)?)*\.[a-z]{2,4}$/i;
                   if(remail.test(this.RegisterForm.email)==false){
                       this.$message('请输入正确的邮箱地址')
                       return false;

                    }
                }
                var params = {'email': this.RegisterForm.email}
                my_email(params).then( res => {
                    if(res.code == 200){
                        this.$message(res.message)
                        
                    }else {
                        this.$message(res.message)
                    }
       
                })
            },
            // 注册
            register() {
              if(this.RegisterForm.username==''){
                    this.$message('用户名不能为空')
                    return false;
       
                }

              if(this.RegisterForm.password!=this.RegisterForm.repassword){
                    this.$message('两次密码不一致')
                    return false;
       
                }

              if(this.RegisterForm.email==''){
                    this.$message('邮箱不能为空')
                    return false;

                }else {
                   var remail=/^[\w\-\.]+@[a-z0-9]+(\-[a-z0-9]+)?(\.[a-z0-9]+(\-[a-z0-9]+)?)*\.[a-z]{2,4}$/i;
                   if(remail.test(this.RegisterForm.email)==false){
                       this.$message('请输入正确的邮箱地址')
                       return false;

                    }
                }

                if(this.RegisterForm.code==''){
                    this.$message('请输入验证码')
                    return false;
       
                }


              var data={'username':this.RegisterForm.username,'password':this.RegisterForm.password,'email':this.RegisterForm.email,'code':this.RegisterForm.code}
              register_post(data).then( res => {
                  if(res.code == 200){
                    this.$message(res.message)
                    this.$router.push({ path: '/login' })
                  }else {
                    this.$message(res.message)
                  }
              })
            }
        }
    }
</script>
 
<style>
  #poster {
    background:url("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1344797418,747230784&fm=26&gp=0.jpg") no-repeat;
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: 0px;
    padding: 0;
  }
 
  .register-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 90px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
 
  .register_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .code_style{
    width:200px;
    margin-left:50px;
  }
 
</style>